package com.bkgy.modularbx.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.modularbx.dictionary.domain.DataDictionary;
import com.bkgy.modularbx.dictionary.dto.DataDictionaryDto;
import com.bkgy.modularbx.dictionary.dto.DataDictionaryUpdateDto;
import com.bkgy.modularbx.dictionary.service.DataDictionaryService;
import com.bkgy.sys.common.entity.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Tag(name = "数据字典管理", description = "钢卷服务模型数据字典接口")
@RestController
@RequestMapping("/dictionary")
@Validated
public class DataDictionaryController {

    @Resource
    private DataDictionaryService dictionaryService;

    @Operation(summary = "新增数据字典", description = "添加新的数据字典记录")
    @PostMapping("/add")
    public ResultData<Void> add(@Parameter(description = "数据字典信息") @RequestBody @Valid DataDictionary dataDictionary) {
        return dictionaryService.addDictionary(dataDictionary);
    }

    @Operation(summary = "分页查询数据字典", description = "根据条件分页查询数据字典信息")
    @PostMapping("/page")
    public ResultData<Page<DataDictionary>> page(@Parameter(description = "查询条件和分页信息") @RequestBody @Valid QueryDTO<DataDictionaryDto> queryDTO) {
        return dictionaryService.pageDictionary(queryDTO);
    }

    @Operation(summary = "删除数据字典", description = "根据ID删除数据字典记录")
    @GetMapping("/delete")
    public ResultData<Void> delete(@Parameter(description = "删除条件") @RequestParam("indocno") Integer indocno) {
        return dictionaryService.deleteDictionary(indocno);
    }

    @Operation(summary = "修改数据字典", description = "更新数据字典信息")
    @PostMapping("/updateDictionary")
    public ResultData<Void> update(@Parameter(description = "更新的数据字典信息") @RequestBody @Valid DataDictionaryUpdateDto dto) {
        DataDictionary dataDictionary = new DataDictionary();
        BeanUtils.copyProperties(dto, dataDictionary);
        return dictionaryService.updateDictionary(dataDictionary);
    }

    @Operation(summary = "查询数据字典详情", description = "根据主键查询数据字典详细信息")
    @PostMapping("/findById")
    public ResultData<DataDictionary> findById(@Parameter(description = "序号") @RequestBody @Valid @NotNull(message = "indocno不可以为空") Integer indocno) {
        return dictionaryService.findById(indocno);
    }
}
